package com.hxht.cmp.module;

import com.github.pagehelper.Page;
import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.User;
import com.hxht.cmp.pojo.CourtroomEx;
import com.hxht.cmp.pojo.FavoritePojo;
import com.hxht.cmp.pojo.FavoriteResult;
import com.hxht.cmp.service.CaseReviewService;
import com.hxht.cmp.service.FavoriteService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @author alice on 2017/8/17 0017.
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/my")
public class UserModule {
    @Resource
    private FavoriteService favoriteService;
    @Resource
    private CaseReviewService caseReviewService;

    @RequestMapping("favorite")
    public ModelAndView favorite() {
        ModelAndView modelAndView = new ModelAndView();
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
        FavoritePojo favoritePojo = new FavoritePojo();
        favoritePojo.setUserUid(user.getUid());
        favoritePojo.setPage(1);
        favoritePojo.setPageSize(12);
        Page<FavoriteResult> favorites = favoriteService.pageFavorite(favoritePojo);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < favorites.getResult().size(); i++) {
            FavoriteResult favoriteResult = favorites.get(i);
            favoriteResult.setStart(format.format(favoriteResult.getStartDate()));
            if (!ObjectUtils.isEmpty(favoriteResult.getCourtRoomUid())) {
                CourtroomEx courtroomEx = Constants.COURTROOM_MAP.get(favoriteResult.getCourtRoomUid());
                favoriteResult.setCourtroomName(courtroomEx.getName());
            }
        }
        modelAndView.addObject("count", favorites.getTotal());
        Map<String, Object> selectJudgeAndSecretary = caseReviewService.selectJudgeAndSecretary(null);
        modelAndView.addObject("judgeList", selectJudgeAndSecretary.get("judgeList"));
        modelAndView.addObject("secretaryList", selectJudgeAndSecretary.get("secretaryList"));
        modelAndView.addObject("courts", Constants.COURT_LIST);
        modelAndView.addObject("courtrooms", Constants.COURTROOM_LIST);
        modelAndView.addObject("caseType", caseReviewService.getAllCaseType());
        modelAndView.addObject("result", favorites.getResult());
        modelAndView.setViewName("user/favorite");
        return modelAndView;
    }

    @RequestMapping("screenshot")
    public ModelAndView screenshot() {
        ModelAndView modelAndView = new ModelAndView();
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("user");
//        modelAndView.addObject("result", result);
        modelAndView.setViewName("user/screenshot");
        return modelAndView;
    }
}
